﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Groups;

namespace VK.Casemeet.Web.ShopAdmin.Groups
{
    public partial class groupEdit : System.Web.UI.Page
    {
        public string Foucsmap = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/ShopAdmin/Home.aspx"},
                new MapPath{ Text="团购列表",Url="grouplist.aspx"},
                new MapPath{ Text="编辑团购信息",Url="#"}
            };
            if (!IsPostBack)
            {
                int groupid = Util.GetQueryInt("groupid");

                txtprocType.DataSource = GroupsBLL.GetCategories(0, "");
                txtprocType.DataValueField = "gc_id";
                txtprocType.DataTextField = "gc_name";
                txtprocType.DataBind();

                SetData(groupid);


            }
        }

        private void SetData(int groupid)
        {
            if (groupid != 0)
            {
                GroupsEntity groups = GroupsBLL.GetGroups(groupid);
                txtTitle.Text = groups.Title;
                txtDiscount.Text = groups.Discount.ToString();
                txtPrice.Text = groups.Price.ToString();
                txtprocType.SelectedValue = groups.ProcTypeID.ToString();
                txtShopID.Text = groups.ShopID.ToString();
                chkIsEnable.Checked = groups.isEnable;
                txtTimes.Value = groups.EndingTime.ToString("yyyy-MM-dd");
                Foucsmap = groups.Foucsmap;
                txtContent.Value = groups.Content;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            var group = SetEntityGroup();
            if (GroupsBLL.UpdateGroup(group) > 0)
            {
                Foucsmap = group.Foucsmap;
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);

            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
        }


        private GroupsEntity SetEntityGroup()
        {
            GroupsEntity group = new GroupsEntity();
            group.ShopID = int.Parse(txtShopID.Text);
            group.Title = txtTitle.Text;
            group.Price = decimal.Parse(txtPrice.Text);
            group.Discount = float.Parse(txtDiscount.Text);
            group.Foucsmap = VK.Comm.Util.GetFormString("hidFouceImgUrl");
            group.EndingTime = DateTime.Parse(txtTimes.Value);
            group.CTime = DateTime.Now;
            group.Content = txtContent.Value;
            group.isEnable = chkIsEnable.Checked;
            group.ProcTypeID = int.Parse(txtprocType.SelectedValue);
            group.GroupID = Util.GetQueryInt("groupid");
            return group;


        }
    }
}